package com.cskaoyan.exception;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author stone
 * @date 2022/06/01 16:08
 */
//@ControllerAdvice
@RestControllerAdvice
public class CustomControllerAdvice {

    // 可以返回ModelAndView:ModelAndView、String
    // 也可以返回Json → @ResponseBody
    // 在形参中可以获得异常对象
    @ExceptionHandler(ArithmeticException.class)
    public String arithmeticExceptionHandle(ArithmeticException exception) {
        return "/exception.jsp";
    }

    @ExceptionHandler(SensitiveWordException.class)
    @ResponseBody
    public BaseRespVo sensitiveWordExceptionHandle(SensitiveWordException exception) {
        String message = exception.getMessage();
        String word = exception.getWord();
        message = message + ":" + word;
        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setMsg(message);
        baseRespVo.setStatus(400);
        return baseRespVo;
    }
}
